package concurrent;

import sun.misc.Unsafe;

/**
 * @Classname ThreadLocalDemo
 * @Description TODO
 * @Date 2021/11/30 22:17
 * @Created by ASUS
 */
public class ThreadLocalDemo {

    private static ThreadLocal<String> local = new ThreadLocal<>();

    public static void main(String[] args) {
        new Thread(() -> {
            System.out.println("设置线程1的ThreadLocal变量值");
            local.set("线程1-value");
            String thread2LocalVal = local.get();
            System.out.println("[Thread1]:" + thread2LocalVal);
            local.remove();
            System.out.println("移除线程1的ThreadLocal变量值");
        }).start();

        new Thread(() -> {
            System.out.println("设置线程2的ThreadLocal变量值");
            local.set("线程2-value");
            String thread2LocalVal = local.get();
            System.out.println("[Thread2]:" + thread2LocalVal);
            local.remove();
            System.out.println("移除线程2的ThreadLocal变量值");
        }).start();

    }
}
